package tables

import (
	"peilian-api/app/global/common"
	"peilian-api/app/global/variable"

	"github.com/gin-gonic/gin"
)

type OptionLog struct { //开始结束视频
	Model
	TraceId     string `json:"trace_id" gorm:"type:varchar(64);not null;default:\"\";comment:请求唯一id"`
	UserId      int64  `json:"user_id" gorm:"type:bigint(20);unsigned;not null;default:0;comment:用户id"`
	UserName    string `json:"user_name" gorm:"type:varchar(255);not null;comment:用户名称"`
	UserAccount string `json:"account_name" gorm:"type:varchar(255);not null;comment:用户账号"`
	IpAddress   string `json:"ip_address" gorm:"type:varchar(16);not null;comment:ip地址"`
	AgentName   string `json:"agent_name" gorm:"type:varchar(255);not null;comment:机构名称"`
	OptionType  string `json:"option_type" gorm:"type:varchar(255);not null;comment:操作类型"`
	OneModule   string `json:"one_module" gorm:"type:varchar(255);not null;comment:一级模块"`
	TwoModule   string `json:"two_module" gorm:"type:varchar(255);not null;comment:二级模块"`
	Number      int64  `json:"number" gorm:"bigint(20);unsigned;not null;default:0;comment:操作数量"`
	ErrInfo     string `json:"err_info" gorm:"type:varchar(1024);not null;comment:错误信息"`
}

func (o *OptionLog) TableName() string {
	return "option_log"
}

// 根据条件查询log
func (o *OptionLog) GetOptionLogByWhere(whereMap map[string]interface{}, where string, odr string, page int, size int) ([]OptionLog, int64, error) {
	var ol []OptionLog
	var count int64 = 0
	if page >= 1 && size > 0 {
		if len(odr) != 0 {
			if d := variable.DB.MySQL.Where(whereMap).Where(where).Offset((page - 1) * size).Limit(size).Order(odr).Find(&ol).Offset(-1).Limit(-1).Count(&count); d.Error != nil {
				return ol, count, d.Error
			}
		} else {
			if d := variable.DB.MySQL.Where(whereMap).Where(where).Offset((page - 1) * size).Limit(size).Find(&ol).Offset(-1).Limit(-1).Count(&count); d.Error != nil {
				return ol, count, d.Error
			}
		}
	} else if len(odr) != 0 {
		if d := variable.DB.MySQL.Where(whereMap).Where(where).Order(odr).Find(&ol).Limit(-1).Count(&count); d.Error != nil {
			return ol, count, d.Error
		}
	} else {
		if d := variable.DB.MySQL.Where(whereMap).Where(where).Find(&ol).Limit(-1).Count(&count); d.Error != nil {
			return ol, count, d.Error
		}
	}
	return ol, count, nil
}

// 插入数据
func (o *OptionLog) InsertOptionLog(c *gin.Context, num int64, s error) error {
	if num == 0 {
		num = 1
	}
	// ctx 获取urL
	uri := c.Request.URL.Path
	ipAddress := c.ClientIP()

	uid, _ := c.Get("uid")
	// 根据uid获取用户信息
	us, _ := GetAgentInfoByUid(uid.(uint))
	errInfo := "ok"
	if s != nil {
		errInfo = s.Error()
	}
	var userId int64 = 0
	if uid == nil {
		userId = 0
	} else {
		userId = int64(uid.(uint))
	}
	userAccount := ""
	if us.AccountName != nil {
		userAccount = *us.AccountName
	}

	var olog = OptionLog{
		TraceId:     "",
		UserId:      userId,
		UserAccount: userAccount,
		UserName:    us.Name,
		IpAddress:   ipAddress,
		AgentName:   us.Agent.AgentName,
		OneModule:   common.UriOptionLogMap[uri][0].(string),
		TwoModule:   common.UriOptionLogMap[uri][1].(string),
		OptionType:  common.UriOptionLogMap[uri][2].(string),
		Number:      num,
		ErrInfo:     errInfo,
	}

	variable.DB.MySQL.Model(&OptionLog{}).Save(&olog)
	return nil
}
